/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.maixiami;

import com.taobao.api.ApiException;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.domain.TaobaokeItem;
import com.taobao.api.request.TaobaokeItemsGetRequest;
import com.taobao.api.response.TaobaokeItemsGetResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;
import org.apache.log4j.Logger;


/**
 *
 * @author qye
 */
public class TaobaokeItemCrawler {
    static Logger logger = Logger.getLogger(ShopCrawler.class);
    
    List<ItemCat> itemCats;
    int fetchInterval = 0;
    
    public TaobaokeItemCrawler(List<ItemCat> itemCats, int fetchInterval) {
        this.itemCats = itemCats;
        this.fetchInterval = fetchInterval;
    }
    
    public void crawl(String outfile) throws FileNotFoundException, IOException, InterruptedException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(outfile));
        
        TaobaokeItemsGetRequest req = new TaobaokeItemsGetRequest();
        req.setFields("num_iid,title,nick,pic_url,price,click_url,commission,commission_rate," +
                "commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume ");
        req.setNick(App.user);
        req.setSort("commissionNum_desc");
        
        boolean start = false;
        for(ItemCat cat : itemCats) {
            if(cat.getIsParent())
                continue;
            if(cat.getCid().equals(50004766L))
                start = true;
            if(!start)
                continue;
            req.setCid(cat.getCid());
            logger.info(cat.getCid());
            // start from 1diamond
            /*for(int i = 7; i < ShopCrawler.LEVELS.length; i += 2) {
                req.setStartCredit(ShopCrawler.LEVELS[i]);
                if(i + 1 < ShopCrawler.LEVELS.length)
                    req.setEndCredit(ShopCrawler.LEVELS[i + 1]);
                else
                    req.setEndCredit(ShopCrawler.LEVELS[i]);
           */
            for(int i = 0; i < 3; i++) {
                int startCreditIndex = 7 + i * 5;
                req.setStartCredit(ShopCrawler.LEVELS[startCreditIndex]);
                if(i < 2)
                    req.setEndCredit(ShopCrawler.LEVELS[startCreditIndex + 4]);
                else
                    req.setEndCredit(ShopCrawler.LEVELS[ShopCrawler.LEVELS.length - 1]);
                
                logger.info(ShopCrawler.LEVELS[startCreditIndex]);
                for(long page = 1; page <= 89; page++) {
                    try {
                        req.setPageNo(page);
                        TaobaoClient client = App.getTaobaoClient();
                        TaobaokeItemsGetResponse res = client.execute(req);
                        if(res.isSuccess()) {
                            
                            if(res.getTaobaokeItems() == null)
                                // no extra pages, break it
                                break;
                            for(TaobaokeItem item : res.getTaobaokeItems()) {
                                oos.writeObject(item);
                            }
                            //logger.info("total results: " + res.getTotalResults() );
                            
                            if(page * 40 > res.getTotalResults())
                                // no extra pages, break it
                                break;
                        }else {
                            logger.warn(res.getMsg());
                            logger.error(res.getErrorCode());
                            if(res.getErrorCode().equals("7")) {
                                oos.close();
                                return;
                            }
                        }
                    }catch(ApiException ex) {
                        logger.error(ex);
                    }
                    
                    if(fetchInterval > 0)
                        Thread.sleep(fetchInterval);
                }
            }// end of for i
            oos.reset();
        }
        
        oos.close();
    }    
}
